package com.cungmua.app.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.cungmua.app.R;
import com.cungmua.app.base.BaseAuthenticatedActivity;
import com.cungmua.app.handler.GetCategoryHandler;
import com.cungmua.app.handler.RequestCityHandler;
import com.cungmua.app.model.City;
import com.cungmua.app.model.User;
import com.cungmua.app.model.request.CityRequest;
import com.cungmua.app.model.request.GetCategoryRequest;
import com.cungmua.app.model.request.LoginRequest;
import com.cungmua.app.model.response.CityResponse;
import com.cungmua.app.model.response.GetCategoryResponse;
import com.cungmua.app.task.GetCategoryTask;
import com.cungmua.app.task.RequestCityTask;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by kelvin.nguyen on 6/16/14.
 */
public class ChooseCityActivity extends BaseAuthenticatedActivity implements RequestCityHandler,AdapterView.OnItemSelectedListener,GetCategoryHandler{

//    private ArrayAdapter<City> adapter;
//    private ListView listCity;
    private List<City> cities=new ArrayList<City>();
    private Spinner spnCity;
    private ArrayList<String> citiesName=new ArrayList<String>();
private int check=0;
    @Override
    public int getLayoutResource() {
        return R.layout.activity_select_city;
    }

    @Override
    public void updateView() {
        CityRequest request = new CityRequest(getDeviceId(), getToken());
        request.setCityType(CityRequest.SELLING);
        request.setCurrentCity(0);

        RequestCityTask task = new RequestCityTask(this, this);
        task.execute(request);
    }


    @Override
    public void setCurrentUser(User data) {

    }



    @Override
    public void loadControls(Bundle savedInstanceState) {
//        listCity = (ListView) findViewById(R.id.listCity);
        spnCity=(Spinner)findViewById(R.id.spinnerCity);
        spnCity.setOnItemSelectedListener(this);
//        listCity.setOnItemClickListener(this);
    }

    @Override
    public void setLastLoginRequest(LoginRequest request) {

    }

    @Override
    public void onListCity(CityResponse response) {
        if (response.getCode() == 0) {
            cities=response.getData();

            setCityList(cities);

            for (City city:cities){
                citiesName.add(city.getName());
            }
           /// citiesName.add(0,"Vui lòng chọn thành phố");
            final ArrayAdapter<String> adapterSize = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item);
            adapterSize.addAll(citiesName);
            spnCity.setAdapter(adapterSize);

//            adapter = new ArrayAdapter<City>(this, R.layout.list_items_city, R.id.txtCityName, response.getData());
//            listCity.setAdapter(adapter);
//            setCityList(response.getData());
        } else {
            alert(response.getMessage());
        }
    }

    private void loadCategories(City city) {
        GetCategoryRequest request = new GetCategoryRequest(getDeviceId(), getToken());
        request.setCityId(city.getId());

        GetCategoryTask task = new GetCategoryTask(this, this);
        task.execute(request);
    }


    @Override
    public void onCategoryList(GetCategoryResponse response) {
        setCategoryList(response.getData());
        goToHotDeal();
        this.finish();
    }

    /**
     * <p>Callback method to be invoked when an item in this view has been
     * selected. This callback is invoked only when the newly selected
     * position is different from the previously selected position or if
     * there was no selected item.</p>
     * <p/>
     * Impelmenters can call getItemAtPosition(position) if they need to access the
     * data associated with the selected item.
     *
     * @param parent   The AdapterView where the selection happened
     * @param view     The view within the AdapterView that was clicked
     * @param position The position of the view in the adapter
     * @param id       The row id of the item that is selected
     */
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        if(check>0) {
            City city = cities.get(position);
            setCurrentCity(city);
            loadCategories(city);
        }else{
            check++;
        }


    }

    /**
     * Callback method to be invoked when the selection disappears from this
     * view. The selection can disappear for instance when touch is activated
     * or when the adapter becomes empty.
     *
     * @param parent The AdapterView that now contains no selected item.
     */
    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
